{include file=member.header.html}
<script type="text/javascript" src="{lib file=My97DatePicker/WdatePicker.js}"></script>
<!-- 内容 -->
<div class="content f-cb">
	<!-- 当前位置 -->
	{include file=path.html}
	<!-- 内容左边栏 -->
	{include file=member.menu.html}
	<!-- 内容右边栏 -->
	<div class="con-shop-r">
		<dl class="promotion-add">
			<dt class="add-title">限时促销</dt>
			<dd>
				<form method="post" action="" id="J_form">
					<table width="100%" class="table-a">
						<tr>
							<td width="80" class="title"> <i class="require">*</i>
								活动名称
							</td>
							<!-- {foreach from=$act_names key=key item=act_name name=act_name} -->
							<td {if $smarty.foreach.act_name.last}{else} width="80"{/if}>
								<input type="radio" name="act_name" value="{$act_name}" id="h_name_{$key}" {if $activity.act_name eq $act_name} checked {/if}>
								<label for="h_name_{$key}">{$act_name}</label>
							</td>
							<!--{/foreach}-->
						</tr>
						<tr>
							<td class="title" valign="top"> <i class="require">*</i>
								活动时间
							</td>
							<td colspan="5">
								<div class="time-f-wrap">
									<input type="text" name="start_time" {if $activity.start_time} disabled="disabled"   value="{$activity.start_time|date:Y-m-d H}" {/if} class="text Wdate" id="time_from"></div>
								<div class="time-f-wrap">
									<input type="text" name="end_time" {if $activity.start_time} value="{$activity.end_time|date:Y-m-d H}" {/if} class="text Wdate" id="time_to"></div>
							</td>
						</tr>
						<tr>
							<td class="title" valign="top">
								<i class="require">*</i>
								优惠方式
							</td>
							<td colspan="5">
								<dl class="promotion-type">
									<dt>
										<input type="checkbox" name="promotion_type" value="1" id="promotion_type_a" {if $activity.discount_type gt 0} checked {/if}>
										<label for="promotion_type_a">限时折扣</label>
									</dt>
									<dd id="promotion_type_wrap">
										<input type="radio" name="discount_type" value="1" id="promotion_type_aa" {if $activity.discount_type eq 1} checked {/if}>
										<label for="promotion_type_aa">打折</label>
										<br />
										<input type="radio" name="discount_type" value="2" id="promotion_type_ab" {if $activity.discount_type eq 2} checked {/if}>
										<label for="promotion_type_ab">一口价</label>
									</dd>
									<dt>
										<input type="checkbox" name="free_shipping" value="1" id="promotion_type_b" {if $activity.free_shipping eq 1} checked {/if}>
										<label for="promotion_type_b">限时包邮（不限购）</label>
									</dt>
								</dl>
							</td>
						</tr>
						<tr>
							<td class="title">
								<i class="require">*</i>
								优惠货品
							</td>
							<td colspan="5">
								<a href="javascript:void(0)" class="subbtnhs" id="J_select">请选择</a>
							</td>
						</tr>
					</table>
					<table width="100%" class="table-b" id="J_fast_set" {if $activity.discount_type eq 2} style="display: none;" {/if}>
						<tr>
							<td width="100" align="center"> <b>批量设置：</b>
							</td>
							<td width="180">
								限时折扣
								<input type="text" name="" class="text" id="J_fast_setA" style="width:80px;" placeholder="请输入" />
								折
							</td>
							<td width="180">
								每人限购数
								<input type="text" name="" class="text" id="J_fast_setB" style="width:80px;" placeholder="不限" />
							</td>
							<td>
								<a href="javascript:void(0)" class="pl-btn" id="J_fast_btn">确定</a>
							</td>
						</tr>
					</table>
					<table width="100%" class="table-c" id="J_selected_list">
						<tr>
							<th width="280">货品描述</th>
							<th width="130">单价（元）</th>
							<th width="150">
								<span id="J_name_type">
									<!--{if $activity.discount_type eq 1}-->
									限时折扣
									<!--{else}-->
									一口价
									<!--{/if}-->
								</span>
							</th>
							<th width="130" {if $activity.discount_type eq 1} style="display:table-cell;" {else} style="display:none;"{/if} >折后价（元）</th>
							<th width="80">限时包邮</th>
							<th width="140">每人限购</th>
							<th>操作</th>
						</tr>
						<!--{if $goods_list}-->
						<!-- {foreach from=$goods_list item=goods} -->
						<tr>
							<td>
								<a target="_blank" href="{$goods.goods_url}" class="desc-wrap linkbtn f-cb" title="{$goods.goods_name|escape}">
									<img src="{$goods.default_image}" alt="{$goods.goods_name|escape}" width="80" height="80" />
									{$goods.goods_name|escape}
								</a>
								<input type="hidden" name="goods_id[]" class="text" value="{$goods.goods_id}" />
								<input type="hidden" name="id[]" value="{$goods.id}">
								<input type="hidden" name="price[]" value="{if $goods.is_district eq 0}{$goods.price}{else}{$goods.min_price}{/if}>"></td>
							<td>
								<!-- 一个价格 -->
								<!-- {if $goods.is_district eq 0}-->
								<span class="gg-price">{$goods.price}</span>
								<!-- {else}-->
								<!-- 多个价格 -->
								<div class="num-price">
									<i class="from">{$goods.min_price}</i>
									~
									<i class="to">{$goods.max_price}</i>
								</div>
								<!-- {/if}-->
							</td>
							<td>
								<div class="discount-type-a" {if $activity.discount_type eq 1} style="display:block;" {else} style="display:none;"{/if}>
									<input type="text" name="promote_discount[]" {if $goods.promote_discount gt 0}value="{$goods.promote_discount}"{/if}  class="text" bjstyle="0" style="width:80px;" placeholder="请输入" onblur="calcDiscount(this)" />
									折
								</div>
								<div class="discount-type-b"  {if $activity.discount_type eq 2} style="display:block;" {else} style="display:none;"{/if}>
									<input type="text" name="promote_price[]" {if $goods.promote_price gt 0}value="{$goods.promote_price}"{/if} class="text" style="width:80px;" placeholder="请输入" />
									元
								</div>
							</td>
							<td {if $activity.discount_type eq 1} style="display:table-cell;" {else} style="display:none;"{/if}>
								<!-- 一个价格 -->
								<!-- {if $goods.is_district eq 0}-->
								<span class="ggAfter-price">{$goods.discount_price}</span>
								<!-- {else}-->
								<!-- 多个价格 -->
								<div class="numAfter-price">
									<i class="from">{$goods.min_discount_price}</i>
									~
									<i class="to">{$goods.max_discount_price}</i>
								</div>
								<!-- {/if}-->
							</td>
							<td>
								<span class="bao-you">
									<!--{if $activity.free_shipping eq 1}-->
									包邮
									<!--{else}-->
									不包邮
									<!--{/if}-->
								</span>
							</td>
							<td>
								<input type="text" name="promote_quantity[]" {if $goods.promote_quantity gt 0}value="{$goods.promote_quantity}"{/if} class="text" style="width:80px;" placeholder="不限" />
							</td>
							<td>
								<a href="javascript:void(0)" class="linkbtn J-del">删除</a>
							</td>
						</tr>
						<!--{/foreach}-->
						<!--{/if}-->
					</table>
					<div id="form_error_info"></div>
					<table class="table-d">
						<tr>
							<td width="90">
								<input type="submit" name="submit" value="保存" class="subbtnhsb" id="J_submit"></td>
							<td>
								<input type="reset" value="重置" class="subbtnhsb" id="J_reset"></td>
						</tr>
					</table>
				</form>
			</dd>
		</dl>
		<!-- 弹出层 -->
		<div class="promotion-pop" id="J_promotion_pop">
			<a href="javascript:void(0)" id="J_pop_close">X</a>
			<p class="title">选择货品</p>
			<ul class="f-cb pop-tab" id="J_tab_ul">
				<li>
					<a href="javascript:void(0)">货品列表</a>
				</li>
				<li>
					<a href="javascript:void(0)">
						已选择的货品(
						<i id="J_tab_bNum">0</i>
						)
					</a>
				</li>
			</ul>
			<div class="tab-wrap" id="J_tab_wrap">
				<div class="tab-wrap-a">
					<table width="100%" class="table-a">
						<tr>
							<td width="60">&nbsp;</td>
							<td width="100">&nbsp;</td>
							<td align="right">
								<select name="cate_id" class="sgcategory" id="J_sgcategory">
									<option value="0">全部分类</option>
									{html_options options=$sgcategories}
								</select>
							</td>
							<td width="240" align="center">
								<input type="text" name="goods_name" class="text" id="J_pop_search" placeholder="请输入" />
							</td>
							<td width="40">
								<a href="javascript:void(0)" class="pl-btn" id="J_search_btn">搜索</a>
							</td>
						</tr>
					</table>
					<div class="table-b-wrap">
						<table width="100%" class="table-b" id="J_pop_tableBA">
							<tr>
								<th width="350">商品名称</th>
								<th width="140">单价（元）</th>
								<th width="140">库存（件）</th>
								<th>操作</th>
							</tr>
						</table>
					</div>
					<table width="100%" class="table-c">
						<tr>
							<td align="right">
								<a href="javascript:void(0)" class="subbtn" id="J_pop_btnA">保存</a>
							</td>
							<td width="80" align="right">
								<a href="javascript:void(0)" class="subbtn" id="J_pop_cancelA">取消</a>
							</td>
						</tr>
					</table>
				</div>
				<div class="tab-wrap-b">
					<table width="100%" class="table-a"></table>
					<div class="table-b-wrap">
						<table width="100%" class="table-b" id="J_pop_tableBB">
							<tr>
								<th width="350">商品名称</th>
								<th width="140">单价（元）</th>
								<th width="140">库存（件）</th>
								<th>操作</th>
							</tr>
						</table>
					</div>
					<table width="100%" class="table-c">
						<tr>
							<td align="right">
								<a href="javascript:void(0)" class="subbtn" id="J_pop_btnB">保存</a>
							</td>
							<td width="80" align="right">
								<a href="javascript:void(0)" class="subbtn" id="J_pop_cancelB">取消</a>
							</td>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="{res file=js/icheck.min.js}"></script>
<script type="text/javascript">
	/*
	*@auto Blade
	*@date 2015-04-17
	*@ 4套数据，2套删除（模板php删，其他js删，更新缓存）
	*/
	var $ajax_goods = {$ajax_goods}; //未加入活动列表的原始数据
	var $select_fid = [];  //已加入活动列表(保存累加，删除时更新)，弹窗数据需要过滤掉此数据
	var $temp_fid = []; //弹窗搜索时过滤已选数据，临时数据，保存时将临时数据插入DOM
	var $del_data = []; //删除时的中间件，辅助已选数据删除单行数据，每次都需重置

	/*弹窗TR-DOM切换*/
	var exchang=function(from,o){
		var $J_tab_bNum=$('#J_tab_bNum');
		var num=parseInt($J_tab_bNum.html());
		var html='';
		var i=$(o).attr('fid');
		var is_district=$ajax_goods[i].is_district;
		var floor_price ='';
		if(is_district == '0'){
			floor_price=$ajax_goods[i].price;
		}else{
			floor_price=$ajax_goods[i].min_price;
		}
		if(from === 'select'){
			num+=1;
			html+='<tr class="pop-tr"><td><a href="'+$ajax_goods[i].goods_url+'" target="_blank" class="desc-wrap linkbtn f-cb"><img src="'+$ajax_goods[i].default_image+'" width="80" height="80" />'+$ajax_goods[i].goods_name+'</a></td><td>'+floor_price+'</td><td>'+$ajax_goods[i].stock+'</td><td><a href="javascript:void(0)" class="linkbtn" onclick="exchang(\'del\',this)" fid="'+i+'">移除</a></td></tr>';
			$('#J_pop_tableBB').append(html);
		}else{
			num-=1;
			html+='<tr class="pop-tr"><td><a href="'+$ajax_goods[i].goods_url+'" target="_blank" class="desc-wrap linkbtn f-cb"><img src="'+$ajax_goods[i].default_image+'" width="80" height="80" />'+$ajax_goods[i].goods_name+'</a></td><td>'+floor_price+'</td><td>'+$ajax_goods[i].stock+'</td><td><a href="javascript:void(0)" class="linkbtn" onclick="exchang(\'select\',this)" fid="'+i+'">选择</a></td></tr>';
			$('#J_pop_tableBA').append(html);
		}
		$J_tab_bNum.html(num);
		$(o).closest('tr').remove();
	};

	/*弹窗重置*/
	var popReset=function(){
		$('#J_pop_tableBA').find('.pop-tr').remove();
		$('#J_pop_tableBB').find('.pop-tr').remove();
		$('#J_tab_bNum').html(0);
		$('#J_pop_search').val('');
		$('#J_promotion_pop').hide();
		wl.hwloverlay();
	};

	/*删除单行数据*/
	var delPromotion = function(o){
		wl.sConfirm({'info':'确定删除这行吗？','success':function(){
			$del_data=[];
			var _fid = $(o).attr('fid');
			for (var i in $select_fid){
				if(i == _fid){
					// console.log('相等，删除这行'+i);
				}else{
					$del_data[i]=$select_fid[i];
				}
			}
			$select_fid = $del_data;
			$(o).closest('tr').remove();
		}});
	};

	/*折扣计算*/
	function accMul(num1,num2){
          var m=0,s1=num1.toString(),s2=num2.toString(); 
	    try{m+=s1.split(".")[1].length}catch(e){};
	    try{m+=s2.split(".")[1].length}catch(e){};
	    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
	}

	var calcDiscount = function(o){
		var freg = /^[1-9]+\d*$|^[1-9]+\d*\.\d{1,1}$/;
		if (!freg.test($(o).val())) {
			$(o).val('');
		}
		var _bjstyle=$(o).attr('bjstyle');
		var _rate=parseFloat($(o).val())/10;
		if(!$(o).val()){
			_rate=1;
		}else if(_rate >= 1 ||  _rate <= 0){
			if($(o).closest('td').find('.input-error').length>0){
				return;
			}
			$(o).closest('td').append('<div class="input-error">格式错误(参考格式:8折)</div>');
			o.timer_id=setTimeout(function(){
				$(o).closest('td').find('.input-error').remove();
			},8000);
			return;
		}else{
			clearTimeout(o.timer_id);
			$(o).closest('td').find('.input-error').remove();
		}
		if(_bjstyle == '0'){
			var prev_price=parseFloat($(o).closest('td').prev('td').find('.gg-price').html());
			var after_price=$(o).closest('td').next('td').find('.ggAfter-price');
			after_price.html(wl.number_format(accMul(_rate,prev_price),2));   
		}else{
			var from_price=parseFloat($(o).closest('td').prev('td').find('.from').html());
			var to_price=parseFloat($(o).closest('td').prev('td').find('.to').html());
			var after_from_price=$(o).closest('td').next('td').find('.from');
			var after_to_price=$(o).closest('td').next('td').find('.to');
			after_from_price.html(wl.number_format(accMul(_rate,from_price),2));   
			after_to_price.html(wl.number_format(accMul(_rate,to_price),2));   
		}
	};

	/*日期插件取当前值*/
	var get_dateValue=function(){
		return document.getElementById('time_from').value;
	};

	/*DOM LOADED*/
	jQuery(function(){
		/*初始化常量*/
		var $promotion_type_a = $('#promotion_type_a'),
			$promotion_type_aa = $('#promotion_type_aa'),
			$promotion_type_ab = $('#promotion_type_ab'),
			$promotion_type_b = $('#promotion_type_b'),
			$J_fast_set = $('#J_fast_set'),
			$J_name_type = $('#J_name_type');

		/*表单提交*/
		$('#J_form').bind('submit',function(){
			var time_from=$('#time_from');
			      time_to=$('#time_to'),
			      form_error_info=$('#form_error_info'),
			      J_list_tr=$('#J_selected_list').find('tr'),
			      pass=false,
			      submit_pass=true;
			form_error_info.empty();
			clearTimeout(this.timer_id);
			if(!time_from.val()){
				submit_pass=false;
				form_error_info.append('<div class="input-error">活动开始时间不能为空</div>');
			}
			if(!time_to.val()){
				submit_pass=false;
				form_error_info.append('<div class="input-error">活动结束时间不能为空</div>');
			}
			if(J_list_tr.length<2){
				submit_pass=false;
				form_error_info.append('<div class="input-error">请选择参加活动的优惠商品</div>');
			}
			if($promotion_type_a.attr('checked')){
				if($promotion_type_aa.attr('checked')){
					$.each($('input[name="promote_discount[]"]'),function(){
						if(!$(this).val()){
							pass=true;
							submit_pass=false;
							return false;
						}
					});
					if(pass){
						form_error_info.append('<div class="input-error">限时折扣输入框不能为空</div>');
					}
				}else{
					$.each($('input[name="promote_price[]"]'),function(){
						if(!$(this).val()){
							pass=true;
							submit_pass=false;
							return false;
						}
					});
					if(pass){
						form_error_info.append('<div class="input-error">一口价输入框不能为空</div>');
					}
				}
			}else{
				if(!$promotion_type_b.attr('checked')){
					submit_pass=false;
					alert('优惠方式必选！');
				}
			}
			if(!submit_pass){
				this.timer_id=setTimeout(function(){
					form_error_info.empty();
				},6000);
			}
			return submit_pass;
		});

		/*表单重置*/
		$('#J_reset').bind('click',function(){
			window.location.reload();
		});

		/*活动时间*/
		$('#time_from').bind('click',function(){
			WdatePicker({dateFmt:'yyyy-MM-dd HH',minDate:'%y-%M-{%d} 00:00',maxDate:'%y-%M-{%d+30} 00:00'});
		});
		$('#time_to').bind('click',function(){
			WdatePicker({dateFmt:'yyyy-MM-dd HH',minDate:get_dateValue()});
		});
		$('#J_tab_ul').wlTabs('#J_tab_wrap > div');

		/*优惠方式*/
		$promotion_type_a.bind('click',function(){
			if($(this).attr('checked')){
				$promotion_type_aa.attr('checked',true);
				$J_fast_set.show();
				$J_name_type.html('限时折扣');
				$('.discount-type-b').hide();
				$('.discount-type-a').show();
			}else{
				$promotion_type_aa.attr('checked',false);
				$promotion_type_ab.attr('checked',false);
			}
		});
		$promotion_type_aa.bind('click',function(){
			$promotion_type_a.attr('checked',true);
			$J_fast_set.show(); //快捷设置
			$J_name_type.html('限时折扣'); //折扣标题
			$('.discount-type-b').hide(); //一口价
			$('.discount-type-a').show(); //折扣
			$('#J_selected_list').find('tr').find('th:eq(3) , td:eq(3)').show();
		});
		$promotion_type_ab.bind('click',function(){
			$promotion_type_a.attr('checked',true);
			$J_fast_set.hide();
			$J_name_type.html('一口价');
			$('.discount-type-a').hide();
			$('.discount-type-b').show();
			$('#J_selected_list').find('tr').find('th:eq(3) , td:eq(3)').hide();
		});
		$promotion_type_b.bind('click',function(){
			if($(this).attr('checked')){
				$('.bao-you').html('包邮');
			}else{
				$('.bao-you').html('不包邮');
			}
		});

		/*批量设置*/
		$('#J_fast_btn').bind('click',function(){
			var $val_a=$.trim($('#J_fast_setA').val()),$val_b=$.trim($('#J_fast_setB').val());
			var freg = /^[1-9]+\d*$|^[1-9]+\d*\.\d{1,1}$/;
			var freg_b = /^[1-9]\d*$/;
			if (!freg.test($val_a)) {
				$('#J_fast_setA').val('');
				$val_a='';
				return;
			}
			if(!freg_b.test($val_b)){
				$('#J_fast_setB').val('');
				$val_b='';
			}
			var _rate=parseFloat($val_a)/10;
			if(!$val_a){
				return;
			}else if(_rate >= 1 ||  _rate <= 0){
				if($(this).closest('td').find('.input-error').length>0){
					return;
				}
				$(this).closest('td').append('<span class="input-error">限时折扣-格式错误(参考格式:8折)</span>');
				this.timer_id=setTimeout(function(){
					$(this).closest('td').find('.input-error').remove();
				},8000);
				return;
			}else{
				clearTimeout(this.timer_id);
				$(this).closest('td').find('.input-error').remove();
			}
			$.each($('input[name="promote_discount[]"]'),function(){
				$(this).val($val_a);
				calcDiscount(this);
			});
			$('input[name="promote_quantity[]"]').val($val_b);
		});

		/*异步删除*/
		$('.J-del').bind('click',function(){
			var _self=$(this);
			wl.sConfirm({'info':'确定删除这行吗？','success':function(){
				var goods_id=_self.closest('tr').children('td:eq(0)').find('input[name="id[]"]').val();
				$.get('index.php?app=my_promotion&act=ajax_dorp_goods',{'id':goods_id},function(jdata){
					if(jdata){
						_self.closest('tr').remove();
					}else{
						alert('删除失败，请重新操作');
					}
				},'json');
			}});
		});

		/*弹窗开启，过滤掉已加入列表的数据*/
		$('#J_select').bind('click',function(){
			var html='';
			$temp_fid=[];
			/*已加入为空时*/
			if($select_fid.length<1){
				for(var i in $ajax_goods){
					if(!$ajax_goods.hasOwnProperty(i)){
                              	continue;
                              }
                              var is_district=$ajax_goods[i].is_district;
					var floor_price ='';
					if(is_district == '0'){
						floor_price=$ajax_goods[i].price;
					}else{
						floor_price=$ajax_goods[i].min_price;
					}
					html+='<tr class="pop-tr"><td><a href="'+$ajax_goods[i].goods_url+'" target="_blank" class="desc-wrap linkbtn f-cb"><img src="'+$ajax_goods[i].default_image+'" width="80" height="80" />'+$ajax_goods[i].goods_name+'</a></td><td>'+floor_price+'</td><td>'+$ajax_goods[i].stock+'</td><td><a href="javascript:void(0)" class="linkbtn" onclick="exchang(\'select\',this)" fid="'+i+'">选择</a></td></tr>';
				}
			}else{
				for(var i in $ajax_goods){
					var bool=true;
					for(var j in $select_fid){
						if(i == $select_fid[j]){ //过滤掉已加入
							bool=false;
							break;
						}
					}
					if(bool){
						var is_district=$ajax_goods[i].is_district;
						var floor_price ='';
						if(is_district == '0'){
							floor_price=$ajax_goods[i].price;
						}else{
							floor_price=$ajax_goods[i].min_price;
						}
						html+='<tr class="pop-tr"><td><a href="'+$ajax_goods[i].goods_url+'" target="_blank" class="desc-wrap linkbtn f-cb"><img src="'+$ajax_goods[i].default_image+'" width="80" height="80" />'+$ajax_goods[i].goods_name+'</a></td><td>'+floor_price+'</td><td>'+$ajax_goods[i].stock+'</td><td><a href="javascript:void(0)" class="linkbtn" onclick="exchang(\'select\',this)" fid="'+i+'">选择</a></td></tr>';
					}
				}
			}
			$('#J_pop_tableBA').append(html);//完成后calc
			var win_width=$(window).width(),
				win_height=$(window).height(),
				pop_width=$('#J_promotion_pop').outerWidth(),
				pop_height=$('#J_promotion_pop').outerHeight();
			wl.swloverlay();
			$('#J_promotion_pop').css({'left':(win_width-pop_width)/2+'px','top':(win_height-pop_height)/2+'px'}).show();
		});

		/*弹窗搜索提交*/
		$('#J_search_btn').bind('click',function(){
			var J_sgcategory = $('#J_sgcategory').val();
			var J_pop_search = $('#J_pop_search').val();
			$temp_fid=[];
			var $all_tr = $('#J_pop_tableBB').find('tr').slice(1);
			if($all_tr.length>0){
				$.each($all_tr,function(){
					var index=$(this).find('a:last').attr('fid');
					$temp_fid[index]=index; //保存临时数据，异步拉数据时过滤
				});
			}
			$.getJSON('index.php?app=my_promotion&act=ajax_get_goods',{'cate_id':J_sgcategory,'goods_name':J_pop_search},function(jdata){
				var html='';
				for(var i in jdata){
					var bool=true;
					for(var j in $select_fid){
						if(i == $select_fid[j]){ //过滤掉已加入
							bool=false;
							break;
						}
					}
					if($all_tr.length>0){
						for(var k in $temp_fid){
							if(i == $temp_fid[k]){ //过滤掉临时加入
								bool=false;
								break;
							}
						}
					}
					if(bool){
						var is_district=jdata[i].is_district;
						var floor_price ='';
						if(is_district == '0'){
							floor_price=jdata[i].price;
						}else{
							floor_price=jdata[i].min_price;
						}
						html+='<tr class="pop-tr"><td><a href="'+jdata[i].goods_url+'" target="_blank" class="desc-wrap linkbtn f-cb"><img src="'+jdata[i].default_image+'" width="80" height="80" />'+jdata[i].goods_name+'</a></td><td>'+floor_price+'</td><td>'+jdata[i].stock+'</td><td><a href="javascript:void(0)" class="linkbtn" onclick="exchang(\'select\',this)" fid="'+i+'">选择</a></td></tr>';
					}
				}
				$('#J_pop_tableBA').find('.pop-tr').remove();
				$('#J_pop_tableBA').append(html);
			});
		});

		/*弹窗保存，保存临时数据，重开弹窗重置*/
		$('#J_pop_btnA , #J_pop_btnB').bind('click',function(){
			$temp_fid=[];
			var $select_list=$('#J_selected_list').find('tr');
			var $all_tr = $('#J_pop_tableBB').find('tr').slice(1);
			if(($select_list.length+$all_tr.length)>21){
				popReset();
				alert('单个活动最多选择20个优惠商品');
				return;
			}
			if($all_tr.length<1){
				popReset();
				return;
			}
			$.each($all_tr,function(){
				var index=$(this).find('a:last').attr('fid');
				$select_fid[index]=index; //保存已选列表（累加）
				$temp_fid[index]=index; //保存临时数据，仅仅在保存时使用
			});
			var html='';
			var baoyou_checked=$promotion_type_b.attr('checked'),$baoyou='不包邮';
			if(baoyou_checked){
				$baoyou='包邮';
			}
			for(var i in $ajax_goods){
				var bool=false;
				for(var j in $temp_fid){ //加入已选中
					if(i == $temp_fid[j]){
						bool=true;
						break;
					}
				}
				if(bool){
					var price_html='',after_price_html='';
					var is_district=$ajax_goods[$temp_fid[i]].is_district;
					var hidden_price='';
					//价格是否区间
					if(is_district == '0'){
						price_html='<span class="gg-price">'+$ajax_goods[$temp_fid[i]].price+'</span>';
						after_price_html='<span class="ggAfter-price">'+$ajax_goods[$temp_fid[i]].price+'</span>';
						hidden_price = $ajax_goods[$temp_fid[i]].price; 
					}else{
						price_html='<div class="num-price"><i class="from">'+$ajax_goods[$temp_fid[i]].min_price+'</i> ~ <i class="to">'+$ajax_goods[$temp_fid[i]].max_price+'</i></div>';
						after_price_html='<div class="numAfter-price"><i class="from">'+$ajax_goods[$temp_fid[i]].min_price+'</i> ~ <i class="to">'+$ajax_goods[$temp_fid[i]].max_price+'</i></div>';
						hidden_price = $ajax_goods[$temp_fid[i]].min_price; 
					}
					//判断外部勾选
					if($promotion_type_aa.attr('checked')){
						html+='<tr><td><a href="'+$ajax_goods[$temp_fid[i]].goods_url+'" target="_blank" class="desc-wrap linkbtn f-cb"><img src="'+$ajax_goods[$temp_fid[i]].default_image+'" width="80" height="80" />'+$ajax_goods[$temp_fid[i]].goods_name+'</a><input type="hidden" name="goods_id[]" class="text" value="'+$ajax_goods[$temp_fid[i]].goods_id+'" /><input type="hidden" name="id[]" value=""><input type="hidden" name="price[]" value="'+hidden_price+'"></td><td>'+price_html+'</td><td><div class="discount-type-a"><input type="text" name="promote_discount[]" class="text" bjstyle="'+is_district+'" style="width:80px;" placeholder="请输入" onblur="calcDiscount(this)" /> 折</div><div class="discount-type-b"><input type="text" name="promote_price[]" class="text" style="width:80px;" placeholder="请输入" /> 元</div></td><td>'+after_price_html+'</td><td><span class="bao-you">'+$baoyou+'</span></td><td><input type="text" name="promote_quantity[]" class="text" style="width:80px;" placeholder="不限" /></td><td><a href="javascript:void(0)" class="linkbtn" fid="'+i+'" onclick="delPromotion(this)">删除</a></td></tr>';
					}else{
						html+='<tr><td><a href="'+$ajax_goods[$temp_fid[i]].goods_url+'" target="_blank" class="desc-wrap linkbtn f-cb"><img src="'+$ajax_goods[$temp_fid[i]].default_image+'" width="80" height="80" />'+$ajax_goods[$temp_fid[i]].goods_name+'</a><input type="hidden" name="goods_id[]" class="text" value="'+$ajax_goods[$temp_fid[i]].goods_id+'" /><input type="hidden" name="id[]" value=""><input type="hidden" name="price[]" value="'+hidden_price+'"></td><td>'+price_html+'</td><td><div class="discount-type-a" style="display:none;"><input type="text" name="promote_discount[]" class="text" bjstyle="'+is_district+'" style="width:80px;" placeholder="请输入" onblur="calcDiscount(this)" /> 折</div><div class="discount-type-b" style="display:block;"><input type="text" name="promote_price[]" class="text" style="width:80px;" placeholder="请输入" /> 元</div></td><td style="display:none;">'+after_price_html+'</td><td><span class="bao-you">'+$baoyou+'</span></td><td><input type="text" name="promote_quantity[]" class="text" style="width:80px;" placeholder="不限" /></td><td><a href="javascript:void(0)" class="linkbtn" fid="'+i+'" onclick="delPromotion(this)">删除</a></td></tr>';
					}
				}
			}
			$('#J_selected_list').append(html);
			popReset();
		});

		/*弹窗取消*/
		$('#J_pop_close , #J_pop_cancelA , #J_pop_cancelB').bind('click',function(){
			popReset();
		});
		//END
	});
</script>
<!-- 页脚 -->
{include file=footer.html}